# -*- coding:utf-8 -*-
import time, hashlib
import os
os.sys.path.append('../../utils/constants/')
from constants import *

class antispam(object):
    "antispam operation"
    def __init__(self, memcache_conn):
        # 配置
        self.__mc_expire_time = 86400

        self.__mc = memcache_conn

    def check_repeat(self, content_item):
        ret = {}
        ret['retcode'] = 0
        ret['message'] = 'OK'
        key = '%s:%s' % ( content_item['uip'], content_item['content'] )
        md5str = hashlib.md5(key).hexdigest()
        mckey = md5str[:16]
        if (content_item.has_key('user_id') and self.__mc.get(mckey) == content_item['user_id']) or \
                ((not content_item.has_key('user_id')) and self.__mc.get(mckey)):
            ret['retcode'] = E_DUP_CONTENT
            ret['message'] = 'duplicate content has commit in 24hours'
        return ret
    def set_content(self, content_item):
        key = '%s:%s' % ( content_item['uip'], content_item['content'] )
        md5str = hashlib.md5(key).hexdigest()
        mckey = md5str[:16]
        self.__mc.add(mckey, content_item.get('user_id', int(time.time())), self.__mc_expire_time)
